package cn.iocoder.yudao.module.knowledge.service.item.impl;

import cn.iocoder.yudao.module.knowledge.service.item.ItemDetailService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.knowledge.controller.admin.item.vo.*;
import cn.iocoder.yudao.module.knowledge.dal.dataobject.item.ItemDetailDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.knowledge.dal.mysql.item.BaseTreeDetailMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.knowledge.enums.ErrorCodeConstants.BASE_TREE_DETAIL_NOT_EXISTS;

/**
 * 知识库明细 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ItemDetailServiceImpl implements ItemDetailService {

    @Resource
    private BaseTreeDetailMapper baseTreeDetailMapper;


    @Override
    public void deleteBaseTreeDetail(Long id) {
        // 校验存在
        validateBaseTreeDetailExists(id);
        // 删除
        baseTreeDetailMapper.deleteById(id);
    }

    @Override
    public void deleteBaseTreeDetailListByIds(List<Long> ids) {
        // 删除
        baseTreeDetailMapper.deleteByIds(ids);
    }


    private void validateBaseTreeDetailExists(Long id) {
        if (baseTreeDetailMapper.selectById(id) == null) {
            throw exception(BASE_TREE_DETAIL_NOT_EXISTS);
        }
    }

    @Override
    public ItemDetailDO getBaseTreeDetail(Long id) {
        return baseTreeDetailMapper.selectById(id);
    }

    @Override
    public PageResult<ItemDetailDO> getBaseTreeDetailPage(ItemDetailPageReqVO pageReqVO) {
        return baseTreeDetailMapper.selectPage(pageReqVO);
    }

    @Override
    public void saveBaseTreeDetail(ItemDetailSaveReqVO reqVO) {
        if (baseTreeDetailMapper.selectById(reqVO.getId()) == null) {
            ItemDetailDO baseTreeDetail = BeanUtils.toBean(reqVO, ItemDetailDO.class);
            baseTreeDetailMapper.insert(baseTreeDetail);
        }else{
            ItemDetailDO updateObj = BeanUtils.toBean(reqVO, ItemDetailDO.class);
            baseTreeDetailMapper.updateById(updateObj);
        }
    }

}